package com.atguigu.flink0922.chapter05.transform;

import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/3/2 15:12
 */
public class Flink04_KeyBy {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(2);
        
        final DataStreamSource<Integer> s1 = env.fromElements(1, 2, 3, 4, 5, 6, 7, 8, 9, 11).setParallelism(1);
        
        s1
            .keyBy(new KeySelector<Integer, Integer>() {
                @Override
                public Integer getKey(Integer value) throws Exception {
                    //                    return value % 2 == 0 ? "偶数" : "奇数";
                    return value % 2 == 0 ? 0 : 1;
                    
                }
            })
            .print();
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
/*
做了一个双重hash

    mm hash
 */